package com.wry.spring.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wry.spring.dao.Order;
import com.wry.spring.mapper.OrderMapper;
import com.wry.spring.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.aop.framework.AopContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *<h1>  <h1>
 *
 * @author wry
 * @since 2025/1/3 10:06
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    public String name;

    @Transactional
    public void v1() {
        v11();
    }


    public void v11() {

    }

    @Resource
    private TransactionTemplate transactionTemplate;

    public void v2() {
        Boolean execute = transactionTemplate.execute(transactionStatus -> {
            try {
                transactionStatus.isNewTransaction();
                System.out.println("业务代码执行");
                transactionStatus.isCompleted();
            }catch (Exception e) {
                // 回滚
                transactionStatus.setRollbackOnly();
                return false;
            }
            return true;
        });
    }

    @Resource
    private DataSourceTransactionManager transactionManager;
    @Resource
    private TransactionDefinition transactionDefinition;

    @GlobalTransactional
    public void v3() {
        TransactionStatus transaction = transactionManager.getTransaction(transactionDefinition);
        try {
            System.out.println("业务代码执行");
//            transactionManager.commit(transaction);
        }catch (Exception e) {
            transactionManager.rollback(transaction);
        }
    }

}
